<#
	Written By: Doug Finke
	Blog: http://dougfinke.com/blog
#>

param (
	$target="install"
)

$regasm = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe"

if(!(Test-Path $regasm)) {
    throw 'Cannot find v4.0 of regasm. Operation aborted'
}

Function Get-Templates ($code) {
    function ql {$args}

    $targetPath = Join-Path $env:HOMEPATH "Documents\Visual Studio 2010\Templates\ItemTemplates"
    $sourcePath = "VS 2010 Templates"
    $templates  = ql PowerShellCSharpCodeGenerator.zip PowerShellSQLCodeGenerator.zip PowerShellTextCodeGenerator.zip

    ForEach ($template in $templates) {
        & $code $template $sourcePath $targetPath
    }
}

Function Remove-PowerShellTemplates {
    Get-Templates {
        param ($template, $source, $target)

        $target = Join-Path $target $template
        if( Test-Path $target) {
            Remove-Item $target -ErrorAction SilentlyContinue
        }
    }
}

Function Copy-PowerShellTemplates {
    Get-Templates {
        param ($template, $source, $target)

        $sourceTemplate = Join-Path $source $template
        if(Test-Path $sourceTemplate) {
            Copy-Item -Path $sourceTemplate -Destination $target -Force
        }
    }
}

switch ($target) {
	"install"   {
		& $regasm /codebase .\PSCodeGen.dll
		Copy-PowerShellTemplates
	}

	"uninstall" {
		& $regasm /u .\PSCodeGen.dll
		Remove-PowerShellTemplates
	}
}